
import React, { useState, useEffect, useRef,useLayoutEffect } from 'react'; // 这行必须写，不然报错，可能是eslint搞事情。
import {
  Row,
  Input,
  Form,
  DatePicker,
  Modal,
  message,
  TreeSelect,
  Radio,
  Select,
  Tree,
  InputNumber
} from 'antd';
const { confirm } = Modal;
const { RangePicker } = DatePicker;
const { TreeNode } = TreeSelect;
const { Option } = Select;
const { TextArea } = Input;

import {updateType } from '@/api/system/dict/type.js'

const EditDept = ({
  visible,
  onCancel,
  oninitList,
  dictId,
  record
}) => {
  const [form] = Form.useForm();
  useResetFormOnCloseModal({
    form,
    visible,
  });

  //点击添加用户
  const onOk = () => {
    form.submit();
  }

  useEffect(() => {
    form.setFieldsValue(record)
  }, [visible])




  //表单提交方法
  const onFinish = values => {
    values.dictId=dictId
    updateType(values).then(res => {
      if (res.code == 200) {
        message.success('修改字典成功');
        onCancel();
        oninitList();
      } else if (res.code == 500) {
        message.warning(res.msg);
      }
    });
  };

  const layout = {
    labelCol: { span: 6 },
    wrapperCol: { span: 16 },
  };


  return (
    <Modal
      title="修改字典"
      visible={visible}
      onOk={onOk}
      onCancel={onCancel}
      cancelText="取消"
      okText="确定"
    >
      <Form
        {...layout}
        form={form}
        labelAlign="right"
        layout="horizontal"
        onFinish={onFinish}
        initialValues={record}

      >

     <Form.Item
          label="字典名称"
          rules={[{ required: true,message:'字典名称不能为空' }]}
          name="dictName"
        >
             <Input placeholder="请输入字典名称"></Input>
        </Form.Item>

        <Form.Item label="字典类型"
          name="dictType"
          rules={[{ required: true, message: '字典类型不能为空' }]}
        >
             <Input placeholder="请输入字典类型"></Input>
        </Form.Item>
        <Form.Item label="状态"
            name="status"
        >
          <Radio.Group>
          <Radio value={'0'}>正常</Radio>
          <Radio value={'1'}>停用</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item label="备注"
          name="remark"
        >
             <TextArea placeholder="请输入内容"></TextArea>
        </Form.Item>
      </Form>
    </Modal>
  );
};


const useResetFormOnCloseModal = ({ form, visible }) => {
  const prevVisibleRef = useRef();
  useEffect(() => {
    prevVisibleRef.current = visible;
  }, [visible]);
  const prevVisible = prevVisibleRef.current;
  useEffect(() => {
    if (!visible && prevVisible) {
      form.resetFields();
    }
  }, [visible]);
};

export default EditDept;
